﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using iShoppingDTO;

namespace iShoppingDAO
{
    public class GiaoDienDAO
    {
        public static GiaoDienDTO SelectGiaoDienMa(String maGiaoDien)
        {
            GiaoDienDTO giaoDienDto = null;
            try
            {

                string sql = "SELECT * FROM GiaoDien WHERE MaGiaoDien= '" + maGiaoDien + "'";
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int i, n = dt.Rows.Count;
                for (i = 0; i < n; i++)
                {
                    giaoDienDto = new GiaoDienDTO();
                    giaoDienDto.MaGiaoDien = dt.Rows[i]["MaGiaoDien"].ToString();
                    giaoDienDto.TenTheme = dt.Rows[i]["TenTheme"].ToString();
                }

            }
            catch (Exception ex)
            {
                giaoDienDto = new GiaoDienDTO();
            }
            return giaoDienDto;
        }
        public static List<GiaoDienDTO> SelectGiaoDies()
        {
            List<GiaoDienDTO> giaoDienDtos = new List<GiaoDienDTO>();
            try
            {

                string sql = "SELECT * FROM GiaoDien WHERE";
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int i, n = dt.Rows.Count;
                for (i = 0; i < n; i++)
                {
                    GiaoDienDTO giaoDienDto = new GiaoDienDTO();
                    giaoDienDto.MaGiaoDien = dt.Rows[i]["MaGiaoDien"].ToString();
                    giaoDienDto.TenTheme = dt.Rows[i]["TenTheme"].ToString();
                    giaoDienDtos.Add(giaoDienDto);
                }

            }
            catch (Exception ex)
            {
                giaoDienDtos = new List<GiaoDienDTO>();
            }
            return giaoDienDtos;
        }
        public static DataTable SelectAllGiaoDienMa()
        {
            DataTable dt = new DataTable();
            try
            {

                string sql = "SELECT * FROM GiaoDien WHERE MaGiaoDien != 'Theme001'";
                dt = DataProvider.ExecuteQuery(sql);
              

            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        public static bool UpdateGiaoDien(GiaoDienDTO giaoDienDto)
        {
            bool res = false;
            try
            {

                string sql = "UPDATE GiaoDien SET TenTheme = @TenTheme WHERE MaGiaoDien = @MaGiaoDien";

                List<SqlParameter> sqlParameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaGiaoDien", SqlDbType.VarChar);
                par[0].Value = giaoDienDto.MaGiaoDien;
                sqlParameters.Add(par[0]);

                par[1] = new SqlParameter("@TenTheme", SqlDbType.NVarChar);
                par[1].Value = giaoDienDto.TenTheme;
                sqlParameters.Add(par[1]);

                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public static bool InsertGiaoDien(GiaoDienDTO giaoDienDto)
        {
            try
            {
                string sql = string.Format("INSERT INTO GiaoDien(MaGiaoDien, TenTheme) VALUES('{0}', '{1}')",
                    giaoDienDto.MaGiaoDien,giaoDienDto.TenTheme);
                bool result = DataProvider.ExecuteNoneQuery(sql);
                return result;
            }
            catch (Exception e)
            {
            }
            return false;
        }
    }
}
